package red.internal.core.codec;

import red.internal.core.message.Protocol;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Jin Zheng
 * @since 2020-05-16
 */
public class MessageCodecFactory
{
	private static volatile MessageCodecFactory factory;

	private Map<Protocol, MessageCodec> codecMap = new HashMap<>();

	private MessageCodecFactory()
	{
		codecMap.put(Protocol.HANDSHAKE, new HandshakeMessageCodec());
		codecMap.put(Protocol.HEARTBEAT, new HeartbeatMessageCodec());
		codecMap.put(Protocol.REGISTRY, new RegistryMessageCodec());
		codecMap.put(Protocol.CACHE, new CacheMessageCodec());
	}

	public static MessageCodecFactory getFactory()
	{
		if (factory == null)
		{
			synchronized (MessageCodecFactory.class)
			{
				if (factory == null)
				{
					factory = new MessageCodecFactory();
				}
			}
		}
		return factory;
	}

	public Map<Protocol, MessageCodec> getCodecMap()
	{
		return codecMap;
	}

}
